﻿using AutoMapper;
using B.S.BasicData.Domain.XU;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.Xu;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.Xu
{
    /// <summary>
    /// 创建箱子
    /// </summary>
    public class CreateBoxHandler : IRequestHandler<CreateBoxCommad, ApiResult<int>>
    {
        private readonly IBaseRepository<BoxModel> _boxbaserepository;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="boxbaserepository">实体</param>
        /// <param name="mapper">中介者</param>
        public CreateBoxHandler(IBaseRepository<BoxModel> boxbaserepository, IMapper mapper)
        {
            _boxbaserepository = boxbaserepository;
            _mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(CreateBoxCommad request, CancellationToken cancellationToken)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            var boxs = _mapper.Map<BoxModel>(request);
            var result = _boxbaserepository.Add(boxs);
            if (result > 0)
            {
                apiResult.Code = ApiEnums.Success;
                apiResult.Msg = "添加成功";
                apiResult.Data = result;

            }
            else 
            {
                apiResult.Code = ApiEnums.Fail;
                apiResult.Msg = "添加失败";
                apiResult.Data = result;
            }
            return Task.FromResult(apiResult); 
        }
    }
}
